import datetime
import os
from time import strftime
import requests
from flask import Flask, render_template, request, session, redirect, url_for, send_from_directory
import pandas as pd
import openpyxl
import xlrd
from datetime import datetime
import db
from forms import LoginForm, RegisterForm, UserAvatarForm
from werkzeug.utils import secure_filename





app = Flask('学生成绩管理')
app.config["SECRET_KEY"] ='TPi4aLWRbyWq8zu9164131'
app.config['UPLOAD_PATH'] = os.path.join(os.path.dirname(__file__),'medias')




#  首页
@app.route('/')
def index():
    return render_template('index.html')

















# 文件下载列表
@app.route('/xiazai')
def xiazai():
    if 'user_name' not in session:
        return redirect(url_for('index'))
    return render_template('xiazai.html')

#  文件下载
@app.route('/downloads1')
def downloads():
    #data_dir = os.path.join(app.root_path,'downloads1')
    fname = '带量采购高值耗材账期明细台账.xlsx'
    return send_from_directory('downloads',fname,as_attachment=True)















#  登录
@app.route('/login',methods=['POST','GET'])
def login():
    if request.method == 'POST':
        user_name = request.form.get('user_name')
        password = request.form.get('password')
        dict = {
            '柳寅' :  'admin',
            '张亚鹏' :  'admin15305605300',
            '赵华' :  'admin13856970615',
            '沈洸淳' : 'admin18055195691',
            '高磊' : 'admin15215699575',
            '孙明礼' : 'admin18715129069',
            '陈璐淼' : 'admin13856034333',
            '殷杰' : 'admin13955142343',
            '邓鹏飞' : 'admin18324746161',
            '王婷' : 'admin18156440880',
            '张弢' : 'admin18256508856',
            '何建利' : 'admin13033087827',
            '刘洪军' : 'admin13515657759',
            '胡少文' : 'admin13365695750',
            '王燕' : 'admin13955121916',
            '齐玲' : 'admin13966684675',
            '尚启云' : 'admin13856964727',
            '金利萍' : 'admin13965135949',
            '潘月瑢' : 'admin13855180196',
            '余祖琳' : 'admin15156695965',
            '汤增洁' : 'admin13665601517',
            '朱利娟' : 'admin18956089627',
            '徐靖' : 'admin13866159519',
            '段明霞' : 'admin13956900313',
            '葛敬梅' : 'admin13013099418',
            '苗家凤' : 'admin13956905503',
            '刘水秀' : 'admin13955185828',
            '张美英' : 'admin15956912943',
            '程玉春' : 'admin13965024409',
            '程远娣' : 'admin13856045231',
            '陈月香' : 'admin18956099781',
            '张云霞' : 'admin13696531604',
            '茅爱芳' : 'admin13865934437',
            '李志敏' : 'admin13515604790',
            '龚腊梅' : 'admin13856933218',
            '孙敬敬' : 'admin13637097632',
            '叶礼芹' : 'admin13956942186',
            '喻萍萍' : 'admin13956096170',
            '姚小燕' : 'admin13865919462',
            '范家莉' : 'admin15618577169',
            '芈晓燕' : 'admin13705606954',
            '郭争艳' : 'admin13309690886',
            '王志平' : 'admin18656158157',
            '孙志宏' : 'admin18326130992',
            '吴彩云' : 'admin18963786164',
            '漏琼' : 'admin13965080244',
            '司胜清' : 'admin13721078732',
            '李俊侠' : 'admin13965038642',
            '徐美玲' : 'admin15256905598',
            '陈孝云': 'admin18019541916',
            '陈晓艳': 'admin13956049387',
            '张诗海': 'admin13856048692',
            '张业平': 'admin13856069679',
            '司雅君': 'admin13966791798',
            '黄丽华': 'admin18326638033',
            '王一真': 'admin18255142132',
            '左淑娟': 'admin15755126012',
            '肖玉梅': 'admin15867188820',
            '汪云云': 'admin18756564810'
            }

        if user_name in dict and password == dict[user_name] :
            session['user_name'] = user_name
            return redirect(url_for('index'))
    return render_template('login.html')

# 退出登录
@app.route('/logout')
def logout():
    session.pop('user_name')
    return redirect(url_for('index'))

























# #  注册新用户
# @app.route('/show_add_user')
# def show_add_user():
#     return render_template('show_add_user.html')
#
# #  接收并处理注册面录入的信息到数据库
# @app.route('/do_add_user',methods=["POST"])
# def do_add_user():
#     # 接收show_add_user页面上录入的信息，并存入相应变量
#     name = request.form.get("name")
#     sex = request.form.get("sex")
#     age = request.form.get("age")
#     email = request.form.get("email")
#     # 将存储页面录入信息的变量，导入对应的数据库表user中
#     sql = f"""
#         insert into user(name,sex,age,email)
#         values ('{name}','{sex}',{age},'{email}')
#     """
#     db.insert_or_update_data(sql)   # 用db里写好的新增和更新函数，将数据录入数据库user表中
#     return 'seuccess'



















# 评分展示
@app.route('/show_grade')
def show_grade():
    if 'user_name' not in session:
        return redirect(url_for('index'))
    df = pd.read_excel('/root/datas/耗材评分表.xlsx',engine='openpyxl')
    return render_template('show_grade.html',table_html = df.to_html(classes='table table-hover table-sm'))

# 评分录入
@app.route('/submit_grade',methods=['POST','GET'])
def submit_grade():
    if 'user_name' not in session:
        return redirect(url_for('index'))
    if request.method == 'POST':
        name = request.form.get('name')
        keshi = request.form.get('keshi')
        lzz = request.form.get('lzz')
        syq = request.form.get('syq')
        xyg = request.form.get('xyg')
        syjt = request.form.get('syjt')
        tmfl = request.form.get('tmfl')
        xddj = request.form.get('xddj')
        jypwq = request.form.get('jypwq')
        shijian = datetime.now()

        df = pd.read_excel('/root/datas/耗材评分表.xlsx',engine='openpyxl')
        new_row = pd.DataFrame({
            '时间' : [shijian],
            '姓名' : session['user_name'],
            '科室' : [keshi],
            '留置针' : [lzz],
            '输液器' : [syq],
            '吸氧管' : [xyg],
            '输液接头':[syjt],
            '透明敷料':[tmfl],
            '心电电极':[xddj],
            '简易喷雾器':[jypwq],
        })
        df = pd.concat([new_row,df])
        df.to_excel('/root/datas/耗材评分表.xlsx',index=False)
        return redirect(url_for('show_grade'))
    return render_template('submit_grade.html')

















# 巡查问题展示
@app.route('/show_xuncha')
def show_xuncha():
    if 'user_name' not in session:
        return redirect(url_for('index'))
    df = pd.read_excel('/root/datas/维修巡查反馈表.xlsx',engine='openpyxl')
    return render_template('show_xuncha.html',table_html = df.to_html(classes='table table-hover table-sm'))

# 巡查问题录入
@app.route('/submit_xuncha',methods=['POST','GET'])
def submit_xuncha():
    if 'user_name' not in session:
        return redirect(url_for('index'))
    if request.method == 'POST':
        name = request.form.get('name')
        keshi = request.form.get('keshi')
        wenti = request.form.get('wenti')
        shijian = datetime.now()

        df = pd.read_excel('/root/datas/维修巡查反馈表.xlsx',engine='openpyxl')
        new_row = pd.DataFrame({
            '时间' : [shijian],
            '姓名' : session['user_name'],
            '科室' : [keshi],
            '向设备科反馈日常问题' : [wenti]
        })
        df = pd.concat([new_row,df])
        df.to_excel('/root/datas/维修巡查反馈表.xlsx',index=False)
        return redirect(url_for('show_xuncha'))
    return render_template('submit_xuncha.html')






















# 公告
@app.route('/zhongbiao')
def zhongbiao():
    return render_template('通知及公告.html')














# 科室成员列表
@app.route("/show_users")
def show_users():
    if 'user_name' not in session:
        return redirect(url_for('index'))
    sql = "select id,name from user"
    datas = db.query_data(sql)
    return render_template("show_users.html",datas=datas)

# 查看单个科室成员信息
@app.route("/show_user/<user_id>")
def show_user(user_id):
    if 'user_name' not in session:
        return redirect(url_for('index'))
    sql = " select * from user where id = " + user_id
    datas = db.query_data(sql)
    user = datas[0]
    return render_template("show_user.html",user=user)








# 文件上传
@app.route('/avatar/upload',methods=['GET','POST'])
def avatar_upload():
    form = UserAvatarForm()
    if form.validate_on_submit():
        img = form.avatar.data
        #f_name = secure_filename(img.filename)
        file_name = os.path.join(app.config['UPLOAD_PATH'],img.filename)
        img.save(file_name)
        return redirect(url_for('success'))
    return render_template('avatar_upload.html',form=form)

# 返回成功页
@app.route('/success')
def success():
    return render_template('success.html')












app.run(host="0.0.0.0", port=5000)
